package com.ykt.mall.kit;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Tools {

	public static void main(String[] args) {
		
		System.out.println(shortUrlBySina("http://localhost:8080/app?id=1&mobileNo=18926066293"));
	}

	public static String getSmsContent(String userName,String md5salt,
			String content,String sign,String phone){

		String time = String.valueOf(System.currentTimeMillis());
		String md5Str = userName + time + content;
		String password = Md5(md5salt+md5Str);
		String s="{";
		s=s+"\"username\""+":"+"\""+userName+"\"," +
				"\"password\""+":"+"\""+password+"\"," +
				"\"content\""+":"+"\""+content+"\"," +
				"\"time\""+":"+"\""+time+"\"," +
				"\"sign\""+":"+"\""+sign+"\"," +
				"\"phone\""+":"+"\""+phone+"\"" ;
		s=s+"}";
		System.out.println("==="+s);
		return s;

	}

	/**
	 * 转换成新浪短网址t.cn
	 */
	public static String shortUrlBySina(String longUrl){

		String shortUrl = "";
		String appkey = "2218733709";
		String sinaUrl = "http://api.t.sina.com.cn/short_url/shorten.json";
		try{
			String result = DoUrl(sinaUrl + "?source="+appkey+"&url_long="+longUrl, "utf-8");

			List<ShortJson> list = new Gson().fromJson(result, new TypeToken<List<ShortJson>>() {
			}.getType());

			shortUrl = list.get(0).url_short;
		}catch(Exception e){
			e.getMessage();
		}

		return shortUrl;
	}

	//{"url_short":"http://t.cn/RvZXTJ3","url_long":"http://reject.sms3.cn:90/phone/loadHomeF?mobile=18926066293","type":0}
	class ShortJson{
		String url_short;

	}

	public static String Md5(String source) {
		String result = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(source.getBytes("UTF-8"));
			byte[] bts = md.digest();
			result = bytes2HexString(bts);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String bytes2HexString(byte[] b) {
		byte[] hex = "0123456789abcdef".getBytes();

		byte[] buff = new byte[2 * b.length];
		for (int i = 0; i < b.length; i++) {
			buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
			buff[2 * i + 1] = hex[b[i] & 0x0f];
		}
		return new String(buff);
	}

	public static void request(String urlString, String content) throws Exception {
		URL url = new URL(urlString);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Content-type", "text/html");
		connection.connect();
		OutputStream os = connection.getOutputStream();
		DataOutputStream dos = new DataOutputStream(os);
		byte[] b = content.getBytes("utf-8");
		dos.write(b);
		if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
			InputStream is = connection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			String responseContent = "";
			while ((line = br.readLine()) != null) {
				responseContent += line;
			}
			br.close();
			isr.close();
			is.close();
			System.out.println("服务端响应内容=======" + responseContent);
		}
	}

	public static String DoUrl(String url,String code){

		String  responseStr = "";
		String sCurrentLine = "";
		InputStream is;  
		URL l_url = null ;
		HttpURLConnection con = null;
		BufferedReader br=null;
		try {
			l_url = new java.net.URL(url);
			con = (HttpURLConnection) l_url.openConnection();
			con.setConnectTimeout(8000);
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(false);
			con.setInstanceFollowRedirects(true);
			con.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			con.setRequestProperty("Content-Type","text/html");
			con.setRequestProperty("Accept-Charset", code);
			con.setRequestProperty("contentType", code);
			con.setRequestMethod("POST");
			con.connect();
			is = con.getInputStream();
			br = new BufferedReader(new InputStreamReader(is, code));
			while ((sCurrentLine = br.readLine()) != null) {
				responseStr += sCurrentLine;
			}

			con = null;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (br != null) {
					br.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return responseStr;
	}
}
